import string


class WordAnalyse:

    def __init__(self, text):

        self.text = text
        self.index = -1
        self.char = ''
        self.word = []
        self.word_list = []
        self.keep_world = []

    def get_char(self):
        self.index += 1
        self.char = self.text[self.index]
        return self.text[self.index]

    def error(self, message):

        print("出错的信息为，未知的单词：" + "".join(self.word) + message)

    def parse_S(self):
        x = self.get_char()
        if x in string.digits:
            self.word.append(x)
            self.parse_D()

        elif x in string.ascii_letters:
            self.word.append(x)
            self.parse_A()
        elif x == '(' or x == ')' or x == '{' or x == '}' or x == ',' or x == '*' or x == ';' or x == '=' or x == '+' or x == '-' or x == '<' or x == '>':
            self.word_list.append(x)
            self.word = []
        elif x == ' ' \
                or x == '\n' or x == '\t':
            if self.word:
                self.word_list.append("".join(self.word))
            self.word = []
        else:
            self.word = []
            self.error(x)

    def parse_D(self):
        x = self.get_char()
        if x in string.digits:
            self.word.append(x)
            self.parse_D()

        elif x == '.':
            self.word.append(x)
            self.parse_S()
        elif x == '(' or x == ')' or x == '{' or x == '}' or x == ',' or x == '*' or x == ';' or x == '=' or x == '+' or x == '-' or x == '<' or x == '>':
            self.word_list.append("".join(self.word))
            self.word_list.append(x)
            self.word = []
        elif x == ' ' \
                or x == '\n' or x == '\t':
            if self.word:
                self.word_list.append("".join(self.word))
            self.word = []
        else:
            self.word = []
            self.error(x)
            return

    def parse_A(self):

        x = self.get_char()
        if x in string.ascii_letters:
            self.word.append(x)
            self.parse_A()
        elif x in string.digits:
            self.word.append(x)
            self.parse_A()
        elif x == '(' or x == ')' or x == '{' or x == '}' or x == ',' or x == '*' or x == ';' or x == '=' or x == '+' or x == '-' or x == '<' or x == '>':
            self.word_list.append("".join(self.word))
            self.word_list.append(x)
            self.word = []
        elif x == ' ' \
                or x == '\n' or x == '\t':
            if self.word:
                self.word_list.append("".join(self.word))
            self.word = []
        else:
            self.word = []
            self.error(x)
            return

    def word_analyse(self):

        """
        识别标识符和数字需要使用一定的文法如下：

        :return:
        """
        while self.index < len(self.text) - 1:
            self.parse_S()

    def get_word_list(self):
        return self.word_list
